/*
 * @Author: cooper
 * @Date: 2025-04-02 18:31:18
 * @Wechat: cmd_develop
 * @Description:
 */
/**
 * @file shell_port.c
 * @author Letter (NevermindZZT@gmail.com)
 * @brief
 * @version 0.1
 * @date 2019-02-22
 *
 * @copyright (c) 2019 Letter
 *
 */

#include "FreeRTOS.h"
#include "task.h"
#include "shell.h"
#include "at32f435_437_clock.h"
#include "./bsp_usart/bsp_usart.h"
#include "shell_port.h"
#include "../bsp_log/log.h"

Shell shell;
char shellBuffer[512];

// 使用尾行模式
void uartLogWrite(char *buffer, short len);
// level 显示的日志等级，高于level的等级不显示，具体的日志等级可以看log.h文件
Log uartLog = {
    .write = uartLogWrite,
    .active = LOG_ENABLE,
    .level = LOG_DEBUG};

/**
 * @brief 用户shell写
 *
 * @param data 数据
 * @param len 数据长度
 *
 * @return short 实际写入的数据长度
 */
short User_Shell_Write(char *data, unsigned short len)
{

    for (int i = 0; i < len; ++i)
    {
        while (usart_flag_get(USART1, USART_TDC_FLAG) == RESET)
            ;
        usart_data_transmit(USART1, data[i]);
    }
    return len;
}

void uartLogWrite(char *buffer, short len)
{
    if (uartLog.shell)
    {
        User_Shell_Write(buffer, len);
    }
}

/**
 * @brief 用户shell初始化
 *
 */
void userShellInit(void)
{
    // 注册自己实现的读写函数
    shell.write = User_Shell_Write;

    // 调用shell初始化函数
    shellInit(&shell, shellBuffer, 512);
    // 添加log初始化
    logRegister(&uartLog, &shell);
}
